package com.pudu.mydemo.act;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;

import android.view.animation.BounceInterpolator;

import android.widget.ImageView;


import com.pudu.mydemo.view.RippleBackground;
import com.pudutech.pdrobot.R;



/**
 * Created by guiliu on 2016/8/3.
 */




public class RipperActivity extends AppCompatActivity implements RippleBackground.OnRipperChangedListener{
    RippleBackground rippleBackground;
    ImageView imageView;

    Activity act;
    Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ripper);
        rippleBackground = (RippleBackground) findViewById(R.id.content);
        imageView = (ImageView) findViewById(R.id.centerImage);

        act = this;
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                playAnimation();
                handler.postDelayed(this, 5000);
            }
        };
//        handler.postDelayed(runnable, 5000);
        playAnimation();
    }



    public void playAnimation() {

        ObjectAnimator scalexAnimator1 = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.6f);
        scalexAnimator1.setDuration(1000);
        ObjectAnimator scalexAnimator2 = ObjectAnimator.ofFloat(imageView, "scaleX", 0.6f ,1.0f);
        scalexAnimator2.setInterpolator(new BounceInterpolator());
        ObjectAnimator scaleYanimator1 = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.6f);
        ObjectAnimator scaleYanimator2 = ObjectAnimator.ofFloat(imageView, "scaleY", 0.6f, 1.0f);


        scaleYanimator2.setInterpolator(new BounceInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.playTogether(scalexAnimator1, scaleYanimator1);
        set.play(scalexAnimator2).with(scaleYanimator2).after(scalexAnimator1);
        set.setDuration(1000);
        set.start();



        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                rippleBackground.stopRippleAnimation();
                rippleBackground.startRippleAnimation();
                rippleBackground.setRippleChangeListener(RipperActivity.this);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
       /* scalexAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.e("update","animation fractio :"+animation.getAnimatedFraction()+"animation value:"+animation.getAnimatedValue()+"animation time:"+animation.getCurrentPlayTime());


            }
        });*/

    }


    @Override
    public void onChanged() {
        Log.e("guiliu","END");
        playAnimation();
    }

    @Override
    public void onRepeat() {
        Log.e("guiliu","onRepeat");
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("guiliu","onKeyDown");
        return super.onKeyDown(keyCode, event);
    }



}